/**
 * Copyright (C) 2010 DustedPixels.com
 */
package com.dustedpixels.cirt.model.cpu.z80;

import com.dustedpixels.cirt.model.core.Circuit;
import com.dustedpixels.cirt.model.core.In;
import com.dustedpixels.cirt.model.core.Internal;
import com.dustedpixels.cirt.model.core.Out;
import com.dustedpixels.cirt.model.core.Pins;
import com.dustedpixels.cirt.model.core.Script;

/**
 * 16-bit register with 16-bit bus.
 * 
 * <p>
 * It's optimized for 16-bit data transfer. 
 *
 * @author Michal Pociecha-Los (micapolos@gmail.com)
 */
@Circuit
public final class Reg16 {
  @In @Pins(16) public int in;
  
  @In public boolean reset;
  @In public boolean writeHi;
  @In public boolean writeLo;
  
  @Out @Pins(16) public int out;
  
  @Internal @Pins(16) public int value;
  
  @Script
  public void update() {
    if (reset) {
      value = 0xFFFF;
    } else {
      if (writeHi) {
        if (writeLo) {
          value = in;
        } else {
          value = (in & 0xFF00) ^ (value & 0x00FF);
        } 
      } else if (writeLo) {
        value = (in & 0x00FF) ^ (value & 0xFF00);
      }
    }
    
    out = value;
  }
}
